const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const path = require('path')

const createWindow = () => {
  const win = new BrowserWindow({
    width: 640,
    height: 480,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // windows 移除菜单
  win.removeMenu()

  // 打开开发者工具
  win.webContents.openDevTools()

  win.loadFile('views/index.html')

  setTimeout(() => {
    win.webContents.send('message', '定时信息')
  }, 5000)
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

ipcMain.on('close', (event) => {
  // app.quit()
  BrowserWindow.fromWebContents(event.sender).send('closeMsg', '关闭失败')
})

ipcMain.handle('upload', async (event) => {
  const res = await dialog.showOpenDialog()
  return res.filePaths[0]
})